package com.cc.moxingvoice.lock;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.cc.moxingvoice.R;
import com.cc.moxingvoice.base.BaseActivity;
import com.cc.moxingvoice.base.BaseLock;
import com.cc.moxingvoice.model.AppBarData;

/**
 * Created by 浩琦 on 2017/6/16.
 * 所有Bar
 */

public class AppBarLock {

    private Context context;
    private BaseLock lock;

    private int leftType;
    private int rightType;

    public AppBarData barData;

    public static final int NULL = 0X0000;
    public static final int BACK = 0X1000;
    public static final int SHARE = 0X9001;
    public static final int OTHER = 0X9999;

    public static final int VOICE_PACKAGE_RIGHT = 0X1001;
    public static final int VOICE_RIGHT = 0X1002;
    public static final int ADD_VOICE_PACKAGE_RIGHT = 0X1003;
    public static final int SUPPORT_AUTHOR= 0X1004;

    public AppBarLock(Context context, BaseLock lock, int title, boolean isBack) {
        this(context, lock, context.getString(title), isBack);
    }

    public AppBarLock(Context context, BaseLock lock, String title, boolean isBack) {
        this.context = context;
        this.lock = lock;
        if (isBack) {
            barData = new AppBarData(title,
                    context.getResources().getDrawable(R.drawable.back), null,
                    null, null,
                    true, false);
            leftType = BACK;
        } else {
            barData = new AppBarData(title,
                    null, null,
                    null, null,
                    false, false);
            leftType = NULL;
        }
    }

    public AppBarLock(Context context, BaseLock lock, int title, int imsLeft, int imsRight, int titleLeft, int titleRight, int leftType, int rightType) {
        this(context, lock, context.getString(title),
                imsLeft, imsRight,
                context.getString(titleLeft), context.getString(titleRight),
                leftType, rightType);
    }

    public AppBarLock(Context context, BaseLock lock, String title, int imsLeft, int imsRight, String titleLeft, String titleRight, int leftType, int rightType) {
        this.context = context;
        this.lock = lock;

        boolean isLeft = false;
        boolean isRight = false;
        if (imsLeft != 0 || titleLeft != null) {
            isLeft = true;
            this.leftType = leftType;
        }
        if (imsRight != 0 || titleRight != null) {
            isRight = true;
            this.rightType = rightType;
        }
        Drawable imgLeft = null;
        Drawable imgRight = null;
        if (imsLeft != 0) {
            imgLeft = context.getResources().getDrawable(imsLeft);
        }
        if (imsRight != 0) {
            imgRight = context.getResources().getDrawable(imsRight);
        }

        barData = new AppBarData(title,
                imgLeft, imgRight,
                titleLeft, titleRight,
                isLeft, isRight);
    }

    public void setTitle(String title) {
        if (title == null) {
            return;
        }
        barData.setTitle(title);
        barData.notifyChange();
    }

    public void leftClick() {
        switch (leftType) {
            case BACK:
                back();
                break;
            case SUPPORT_AUTHOR:
                ((VoicePackageLock) lock).donation();
                break;
            case OTHER:

                break;
        }
    }

    public void rightClick() {
        switch (rightType) {
            case VOICE_PACKAGE_RIGHT:
                boolean mainManage = ((VoicePackageLock) lock).appBarRightManage();
                if (mainManage) {
                    barData.setTitleRight("完成");
                } else {
                    barData.setTitleRight("管理");
                }
                barData.notifyChange();
                break;
            case VOICE_RIGHT:
                boolean VoiceManage = ((VoiceLock) lock).appBarRightManage();
                if (VoiceManage) {
                    barData.setTitleRight("完成");
                } else {
                    barData.setTitleRight("管理");
                }
                barData.notifyChange();
                break;
            case ADD_VOICE_PACKAGE_RIGHT:
                ((AddVoicePackageLock) lock).appBarRightDone();
                break;
            case SHARE:
                ((DonationLock) lock).share();
                break;
            case OTHER:

                break;
        }
    }

    private void back() {
        ((BaseActivity) context).finish();
    }

}
